<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/7/15
 * Time: 15:39
 */

namespace Admin\Controller;
use Common\Model\OptionModel;

class FuturesBiteOrderController extends AdminController{
    public function __construct(){
        parent::__construct();
    }

    public function search_string(){
        $status=I('get.status');
        if($status && $status!=-1){
    			$where['status']=intval($status);
    			$this->assign('status',$status);
    		}
    		$direction=I('get.direction');
    		if($direction && $direction!=-1){
    			if($direction == 2){
    				$where['direction']=0;
    			}else{
    				$where['direction']=$direction;
    			}
    			$this->assign('direction',$direction);
    		}
        $name=I('get.name');
        if($name){
            $user['nickname']=array('like','%'.$name.'%');
            $userdata=M('follow')->where($user)->select();
            $arr=array();
            foreach($userdata as $v){
                $arr[]=$v['id'];
            }
            $where['user_id']=array('in',$arr);
            $this->assign('name',$name);
        }
        $option=I('get.option');
        if($option){
            $optionName['name']=array('like','%'.$option.'%');
            $userdata=M('futures_option')->where($optionName)->select();
            $optionarr=array();
            foreach($userdata as $value){
                $optionarr[]=$value['id'];
            }
            $where['option_id']=array('in',$optionarr);
            $this->assign('option',$option);
        }
        $start_time=I('get.start_time');
        if($start_time){
            $start_time=strtotime($start_time);
            $where['start_time']=array('gte',$start_time);
            $this->assign('start_time',$start_time);
        }else{
            $start_time=strtotime(date('Y-m-d'));
            $this->assign('start_time',$start_time);
        }
        $end_time=I('get.end_time');
        if($end_time){
            $end_time=strtotime($end_time)+86400;
            $where['start_time']=array('lte',$end_time);
            $this->assign('end_time',$end_time);
        }else{
            $end_time=time();
            $this->assign('end_time',$end_time);
        }
        if($start_time && $end_time){
            $where['start_time']=array('$gte'=>$start_time,'$lte'=>$end_time);
        }
        return $where;
    }

    public function index(){
        $where=$this->search_string();
        $Model=new OptionModel('biteorder');
        $size=$Model->where($where)->count();
        $page=new \Think\Page($size,20);
        $odata=$Model->where($where)->limit($page->firstRow.','.$page->listRows)->order('_id desc')->select();
        foreach($odata as $key=>$val){
            $username=M('follow')->where('id='.$val['user_id'])->find();
            $opname=M('futures_option')->where('id='.$val['option_id'])->find();
            $odata[$key]['opname']=$opname['name'];
            $odata[$key]['code']=$opname['code'];
            $odata[$key]['user_name']=$username['nickname'];
        }
        $mkarr['maker_id']=array('eq',0);
        $makeId=M('follow')->where($mkarr)->select();
        $mId=array();
        foreach($makeId as $mk){
            $mId[]=$mk['id'];
        }
        $where['user_id']=array('nin',$mId);
        $name=I('get.name');
        if($name){
            $user['nickname']=array('like','%'.$name.'%');
            $userdata=M('follow')->where($user)->select();
            $arr=array();
            foreach($userdata as $v){
                $arr[]=$v['id'];
            }
            $where['user_id']=array('in',$arr);
            $this->assign('name',$name);
        }
        $showdata=$Model->where($where)->select();
        $topmoney="";
        $ying="";
        $kui="";
        foreach($showdata as $v){
            $topmoney+=$v['invest'];
            if($v['profit']>0){
                $ying+=$v['profit']-$v['invest'];
            }else{
                $kui+=$v['profit'];
            }
        }
        $topmoney = round($topmoney,2);
        $ying = round($ying,2);
        $this->assign('topmoney',$topmoney);
        $this->assign('ying',$ying);
        $this->assign('kui',$kui);
        $this->assign('odata',$odata);
        $this->assign('page',$page);
        $this->display();
    }
    public function down_ouyuan(){
  		$where=$this->search_string();
  		Vendor('PHPExcel');
  		Vendor('Autoloader');
  		$Model=new OptionModel('goldorder');
  		$odata=$Model->where($where)->select();
  		/*实例化phpexecl类*/
  		$objPHPExcel = new \PHPExcel();
  //		$objPHPExcel->getActiveSheet()->getStyle('C')->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
  		$objPHPExcel->setActiveSheetIndex(0)
  				//Excel的第一列的名称
  				->setCellValue('A1', 'ID')
  				->setCellValue('B1', '姓名')
  				->setCellValue('C1', '合约')
  				->setCellValue('D1', '合约代码')
  				->setCellValue('E1', '金额')
  				->setCellValue('F1', '当前卖价')
  				->setCellValue('G1', '平仓买价')
  				->setCellValue('H1', '方向')
  				->setCellValue('I1', '盈利')
  				->setCellValue('J1', '时长')
  				->setCellValue('K1', '收益率')
  				->setCellValue('L1', '状态')
  				->setCellValue('M1', '开始时间')
  				->setCellValue('N1', '结束时间');
  		/*将数据放在phpexecl表中*/
  		foreach($odata as $key=>$val){
  			$username=M('follow')->where('id='.$val['user_id'])->find();
  			$opname=M('futures_option')->where('id='.$val['option_id'])->find();
  			$odata[$key]['opname']=$opname['name'];
  			$odata[$key]['code']=$opname['code'];
  			$odata[$key]['user_name']=$username['nickname'];
  		}
  		$num=2;
  		foreach ($odata as $k => $value) {
  			$objPHPExcel->setActiveSheetIndex(0)
  					//Excel的第A列，uid是你查出数组的键值，下面以此类推
  					->setCellValue('A' . $num, $value['id'])
  					->setCellValue('B' . $num, $value['user_name'])
  					->setCellValue('C' . $num, $value['opname'])
  					->setCellValue('D' . $num, $value['code'])
  					->setCellValue('E' . $num, $value['invest'] )
  					->setCellValue('F' . $num, $value['buy_price'])
  					->setCellValue('G' . $num, $value['sell_price'])
  					->setCellValue('H' . $num, $value['direction']==1?"看涨":"看跌")
  					->setCellValue('I' . $num, $value['profit'])
  					->setCellValue('J' . $num, $value['duration'])
  					->setCellValue('K' . $num, $value['yield_rate'])
  					->setCellValue('L' . $num, $value['status']==1?"进行中":"已结束")
  					->setCellValue('M' . $num, date('Y-m-d H:i:s',$value['start_time']))
  					->setCellValue('N' . $num, date('Y-m-d H:i:s',$value['end_time']));
  			$num++;
  		}

  		/*以下就是对处理Excel里的数据， 横着取数据，主要是这一步，其他基本都不要改*/
  		$objPHPExcel->getActiveSheet()->setTitle('佣金明细');
  		$objPHPExcel->setActiveSheetIndex(0);
  		$time = date("Y/m/d");
  		$head = $time."美元订单xls";
  		header('Content-Type: application/vnd.ms-excel');
  		header('Content-Disposition: attachment;filename='.$head.'');
  		header('Cache-Control: max-age=0');
  		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  		$objWriter->save('php://output');
  		exit;
  	}
    public function delouyuan(){
      $Model = new OptionModel('goldorder');		//实例化MongoDB
      if(IS_POST){
        $ids = $_POST['ids'];
        foreach($ids as $v){
          $id= $v;
          $where = array('id'=>(int)$id);
          $data = $Model->where($where)->delete();
        }
        $this->success('删除成功',U('FuturesGoldOrder/index'));
      }
    }
}
